#include "VideoWidget.h"

VideoWidget::VideoWidget(QWidget *parent) : QWidget(parent)
{
    setAttribute(Qt::WA_OpaquePaintEvent);
}

VideoWidget::~VideoWidget()
{

}

void VideoWidget::displayFrame(const QImage &frame)
{
    m_currentFrame = frame;
    update();
}

void VideoWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    if (!m_currentFrame.isNull()) {
        // 缩放图像以适应窗口大小
        QImage scaled = m_currentFrame.scaled(size(), Qt::KeepAspectRatio);
        painter.drawImage((width() - scaled.width()) / 2,
                         (height() - scaled.height()) / 2,
                         scaled);
    } else {
        painter.fillRect(rect(), Qt::darkGray);
    }

    event->accept();
}
